import Vue from 'vue';
import {
    url,
} from '@/lib/utils';
import {
    api,
} from '@/lib/api';

import { Module } from 'vuex';
import { RootState } from '@/types/store';

export interface CrowdState {
    /** 礼包 */
    product: ListWrap<any> & ListReq;
    /** 众筹列表 */
    list: ListWrap<any> & ListReq;
    /** 众筹详情 */
    detail: any;
    /** 支持列表 */
    supportList: any;
    /** 众筹商品详情 */
    goodDetail: any;
}

const m: Module<CrowdState, RootState> = {
    namespaced: true,
    state: {
        product: {
            list: [],
            loaded: false,
            req: false,
        },
        list: {
            list: [],
            loaded: false,
            req: false,
        },
        detail: {},
        supportList: [],
        goodDetail: {},
    },
    mutations: {
        /**
         * 众筹礼包列表
         */
        concatProductList(state, list) {
            state.product.list.push(...list);
        },
        setProductLoaded(state, loaded) {
            state.product.loaded = loaded;
        },
        setProductReq(state, req) {
            state.product.req = req;
        },
        /**
         * 众筹列表
         */
        concatList(state, list) {
            state.list.list.push(...list);
        },
        setListLoaded(state, loaded) {
            state.list.loaded = loaded;
        },
        setListReq(state, req) {
            state.list.req = req;
        },
        /**
         * 记录众筹详情
         */
        setDetail(state, { crowdId, detail }) {
            Vue.set(state.detail, crowdId, detail);
        },
        /**
         * 记录支持列表
         */
        setSupportList(state, supportList) {
            state.supportList = supportList;
        },
        /**
         * 记录众筹商品详情
         */
        setGoodDetail(state, { id, goodDetail }) {
            Vue.set(state.goodDetail, id, goodDetail);
        },
    },

    actions: {
        /**
         * 获取商品详情
         */
        async getProductList({ state, commit }, { offset, count }) {
            const wrap = state.product;
            if (wrap.loaded || wrap.req) {
                return;
            }
            const end = offset + count;
            const len = wrap.list.length;
            if (end <= len) {
                return;
            }
            if (offset < len) {
                offset = len;
                count = end - offset;
            }
            commit('setProductReq', true);
            const res = await api.get({
                url: `${url}/api/crowd/good`,
                data: {
                    offset,
                    count,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('concatProductList', list);
                if (list.length < count) {
                    commit('setProductLoaded', true);
                }
            }
            commit('setProductReq', false);
            return res;
        },
        /**
         * 创建众筹
         */
        async create(context, { id }) {
            const res = await api.post({
                url: `${url}/api/crowd`,
                data: {
                    id,
                },
            });
            return res;
        },
        /**
         * 获取众筹列表
         */
        async getList({ state, commit }, { offset, count }) {
            const wrap = state.list;
            if (wrap.loaded || wrap.req) {
                return;
            }
            const end = offset + count;
            const len = wrap.list.length;
            if (end <= len) {
                return;
            }
            if (offset < len) {
                offset = len;
                count = end - offset;
            }
            commit('setListReq', true);
            const res = await api.get({
                url: `${url}/api/crowd`,
                data: {
                    offset,
                    count,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('concatList', list);
                if (list.length < count) {
                    commit('setListLoaded', true);
                }
            }
            commit('setListReq', false);
            return res;
        },
        /**
         * 获得众筹详情
         */
        async getDetail(context, { crowdId, forceUpdate }) {
            if (context.state.detail[crowdId] && !forceUpdate) {
                return;
            }
            const res = await api.get({
                url: `${url}/api/crowd/detail`,
                data: {
                    id: crowdId,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                context.commit('setDetail', {
                    crowdId,
                    detail: res.data.data,
                });
            }
            return res;
        },
        /**
         * 获得支持列表
         */
        async getSupportList(context, { crowdId }) {
            const res = await api.get({
                url: `${url}/api/crowd/support`,
                data: {
                    id: crowdId,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                context.commit('setSupportList', res.data.data);
            }
            return res;
        },
        /**
         * 创建支持
         */
        async support(context, { id, money, content }) {
            const res = await api.post({
                url: `${url}/api/crowd/support`,
                data: {
                    no: id,
                    money,
                    content,
                },
            });
            return res;
        },
        /**
         * 获得众筹商品详情
         */
        async getGoodDetail(context, { id }) {
            const res = await api.get({
                url: `${url}/api/crowd/good_detail`,
                data: {
                    id,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                context.commit('setGoodDetail', {
                    id,
                    goodDetail: res.data.data,
                });
            }
            return res;
        },
    },
};

export default m;
